package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardPosEntity;
import city.spring.modules.cardinfo.service.CardPosService;
import city.spring.modules.system.entity.OrganizationEntity;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.OrganizationService;
import city.spring.modules.system.service.UserService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：消费机
 *
 * @author Panxm
 * @date 2020-04-14 09:34:21
 */
@RestController
@RequestMapping("/card_pos")
public class CardPosController extends ApiController {
    private final CardPosService cardPosService;
    private final UserService userService;
    private final OrganizationService organizationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardPosEntity, ?>> entityOrderFields;

    public CardPosController(CardPosService cardPosService, UserService userService, OrganizationService organizationService) {
        this.cardPosService = cardPosService;
        this.userService = userService;
        this.organizationService = organizationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardPosEntity::getId);
        entityOrderFields.add(CardPosEntity::getNo);
        entityOrderFields.add(CardPosEntity::getName);
        entityOrderFields.add(CardPosEntity::getType);
        entityOrderFields.add(CardPosEntity::getDept);
        entityOrderFields.add(CardPosEntity::getManager);
        entityOrderFields.add(CardPosEntity::getConsumptionAmount);
        entityOrderFields.add(CardPosEntity::getDeductionFee);
        entityOrderFields.add(CardPosEntity::getEffectiveUser);
        entityOrderFields.add(CardPosEntity::getOpenMealAmount);
        entityOrderFields.add(CardPosEntity::getOnlineNum);
        entityOrderFields.add(CardPosEntity::getSingleNum);
        entityOrderFields.add(CardPosEntity::getTimeMoney);
        entityOrderFields.add(CardPosEntity::getIpAddress);
        entityOrderFields.add(CardPosEntity::getOnlineStates);
        entityOrderFields.add(CardPosEntity::getOnlineTime);
        entityOrderFields.add(CardPosEntity::getOpenTime);
        entityOrderFields.add(CardPosEntity::getConsumptionPattern);
        entityOrderFields.add(CardPosEntity::getTotalPay);
        entityOrderFields.add(CardPosEntity::getStates);
    }

    /**
     * 获取全部的 <strong>消费机</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardPosEntity entity) {
        LambdaQueryChainWrapper<CardPosEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>消费机</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardPosEntity entity) {
        LambdaQueryChainWrapper<CardPosEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardPosEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (CardPosEntity record : page.getRecords()) {
            try {
                OrganizationEntity organizationEntity = organizationService.getById(record.getDept());
                record.setDept(organizationEntity.getTitle());
                UserEntity userEntity = userService.getById(record.getManager());
                record.setManager(userEntity.getNickname());
            } catch (Exception ignore) {
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>消费机</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        CardPosEntity entity = cardPosService.getById(id);

        // TODO 在这里获取所有关联的 employees 信息列表
        entity.setMeals(null);
        cardPosService.loadMeals(entity);
        return success(entity);
    }

    /**
     * 添加一个 <strong>消费机</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody CardPosEntity entity) {
        cardPosService.saveCardPos(entity);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>消费机</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody CardPosEntity entity) {
        entity.setId(id);
        cardPosService.updateCardPos(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>消费机</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        cardPosService.deleteCardPos(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>消费机</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        cardPosService.deleteCardPos(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardPosEntity> buildLambdaQuery(CardPosEntity entity) {
        LambdaQueryChainWrapper<CardPosEntity> lambdaQuery = cardPosService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardPosEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getNo()), CardPosEntity::getNo, entity.getNo());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), CardPosEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), CardPosEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDept()), CardPosEntity::getDept, entity.getDept());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getManager()), CardPosEntity::getManager, entity.getManager());
        lambdaQuery.like(entity.getConsumptionAmount() != null, CardPosEntity::getConsumptionAmount, entity.getConsumptionAmount());
        lambdaQuery.like(entity.getDeductionFee() != null, CardPosEntity::getDeductionFee, entity.getDeductionFee());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getEffectiveUser()), CardPosEntity::getEffectiveUser, entity.getEffectiveUser());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOpenMealAmount()), CardPosEntity::getOpenMealAmount, entity.getOpenMealAmount());
        lambdaQuery.like(entity.getOnlineNum() != null, CardPosEntity::getOnlineNum, entity.getOnlineNum());
        lambdaQuery.like(entity.getSingleNum() != null, CardPosEntity::getSingleNum, entity.getSingleNum());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTimeMoney()), CardPosEntity::getTimeMoney, entity.getTimeMoney());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIpAddress()), CardPosEntity::getIpAddress, entity.getIpAddress());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOnlineStates()), CardPosEntity::getOnlineStates, entity.getOnlineStates());
        lambdaQuery.like(entity.getOnlineTime() != null, CardPosEntity::getOnlineTime, entity.getOnlineTime());
        lambdaQuery.like(entity.getOpenTime() != null, CardPosEntity::getOpenTime, entity.getOpenTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getConsumptionPattern()), CardPosEntity::getConsumptionPattern, entity
                .getConsumptionPattern());
        lambdaQuery.like(entity.getTotalPay() != null, CardPosEntity::getTotalPay, entity.getTotalPay());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getStates()), CardPosEntity::getStates, entity.getStates());
        return lambdaQuery;
    }

}
